package com.centling.smartSealForPhone.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import com.centling.smartSealForPhone.BuildConfig
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.application.SmartSeal
import com.centling.smartSealForPhone.constants.SPConstants
import com.centling.smartSealForPhone.utils.SPUtil
import com.centling.smartSealForPhone.utils.ShowToast
import kotlinx.android.synthetic.publicCloud.activity_set_server_ip.*

/**
 * 设置Ip
 */
class SetServerIpActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_set_server_ip)

        initView()
    }

    fun initView() {
        if (!SPUtil.getString(SPConstants.IPADDRESS).startsWith("http")) {
            et_set_server_ip.setText(SmartSeal.getInstance().ip)
        }
        tv_set_server_sure.setOnClickListener {
            val newIp = et_set_server_ip.text.toString()
            if (!TextUtils.isEmpty(newIp)) {
                SmartSeal.getInstance().changeIp(newIp)
                setResult(Activity.RESULT_OK,  Intent().putExtra("ip", newIp))
                finish()
            } else {
                ShowToast.shortTime("租户号不能为空")
            }
        }
        tv_set_server_cancel.setOnClickListener { finish() }
    }

    override fun onBackPressed() {
        setResult(Activity.RESULT_CANCELED)
        finish()
    }
}
